class_name Weapon
extends EquipableItem



@onready var sprite: Sprite2D = $Sprite2D
@onready var shoot_pos_marker: Marker2D = $ShootPosMarker
@onready var animation_player: AnimationPlayer = $AnimationPlayer
@onready var bullet_op: ObjectPool = $OP_Bullet
## 通过配置hit_effect_op节点的scene字段来实现子弹命中时的效果，若未指定需要实例化的scene则无命中效果
@onready var hit_effect_op: ObjectPool = $OP_HitEffect

@export var rotate_follow_mouse: bool = true
# @export var can_charging_attack: bool = false

# @export var damage_info: DamageInfo
@export var shoot_range: float
@export var shoot_speed: float = 1:
    get:
        if not animation_player: return 0
        return animation_player.speed_scale

    set(value):
        if not animation_player: return
        animation_player.speed_scale = value
        
# @export var bullet_scene: PackedScene

@export var bullet_start_pos_is_mouse_pos: bool
@export var bullet_end_pos_is_mouse_pos: bool
@export var bullet_speed: float


@export_group("数值设置")
@export var damage_attribute: DamageInfo.DamageAttribute:
    set(value):
        if damage_attribute == value: return
        damage_attribute = value
        damage_attribute_changed.emit(damage_attribute)

@export var attack_addition: float
@export var normal_damage_addition: float
@export var fire_damage_addition: float
@export var ice_damage_addition: float
@export var thunder_damage_addition: float



var _attack_direction: Vector2

# signal creating_bullet(bullet: Bullet)
signal damage_attribute_changed(new: DamageInfo.DamageAttribute)


func _ready() -> void:
    damage_attribute_changed.connect(_on_damage_attribute_changed)


# 绘制攻击范围
func _draw() -> void:
    var color := DamageInfo.get_damage_attribute_clr(damage_attribute)
    draw_circle(shoot_pos_marker.position, shoot_range, color, false)


#region animation_player中函数轨道调用的函数
# func _set_bullet_start_pos() -> void:
#     pass


func _set_attack_direction() -> void:
    _attack_direction = shoot_pos_marker.global_position.direction_to(get_global_mouse_position())


func _create_attack_effect() -> Bullet:
    var bullet := bullet_op.acquire() as Bullet
    
    # var player := get_tree().get_first_node_in_group("player") as Player
    var player := Global.get_player()
    var info := DamageInfo.create(
        player, null,
        player.data.attack.value + attack_addition, damage_attribute,
        player.data.critical_rate.value, player.data.critical_degree.value,
        true, false, false, false
    )

    var op = hit_effect_op if hit_effect_op.scene else null
    bullet.setup(info, bullet_speed, shoot_range, shoot_pos_marker.global_position, _attack_direction, op)
    bullet.z_index = z_index + 1

    return bullet

#endregion


## 虚函数，实现伤害属性改变时的效果
func _on_damage_attribute_changed(_new_attr: DamageInfo.DamageAttribute) -> void:
    pass


func attack() -> void:
    animation_player.play("attack")


# func charging_attack() -> void:
#     pass


func in_range(pos: Vector2) -> bool:
    return shoot_pos_marker.global_position.distance_squared_to(pos) <= shoot_range ** 2


## 虚函数
func equip(caller: Character) -> void:
    if is_equipped: return
    # caller.data.attack.add_modifier(attack_addition)
    caller.data.normal_additions.append(normal_damage_addition)
    caller.data.fire_additions.append(fire_damage_addition)
    caller.data.ice_additions.append(ice_damage_addition)
    caller.data.thunder_additions.append(thunder_damage_addition)
    shoot_speed = caller.data.attack_speed.value
    is_equipped = true
    pass


## 虚函数
func remove(caller: Character) -> void:
    if not is_equipped: return
    # caller.data.attack.add_modifier(-attack_addition)
    caller.data.normal_additions.erase(normal_damage_addition)
    caller.data.fire_additions.erase(fire_damage_addition)
    caller.data.ice_additions.erase(ice_damage_addition)
    caller.data.thunder_additions.erase(thunder_damage_addition)
    is_equipped = false
    pass
